/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import IO.PlayMP3;
import IO.ResourceCharacterLocation;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;

/**
 *
 * @author mvinicio
 */
public class CharacterActionForm extends javax.swing.JFrame {

    DraggableImageComponent imageComponent;
    String accion = "";
    MainMenu menu;
    String modo = "";
    int actionId;
    String selectedCharacterType;

    public CharacterActionForm(DraggableImageComponent dic, MainMenu main) {
        modo = "Crear";
        this.selectedCharacterType = dic.selectedCharacterType;
        initComponents();
        setButtonImages();
        this.getContentPane().setBackground(Color.white);
        imageComponent = dic;
        menu = main;
        btnWalkForward.doClick();
        jSlider1.setValue(Integer.parseInt(Configuration.Configuration.SliderDefault.get("speed")));
        lblVelocityValue.setText(Configuration.Configuration.SliderDefault.get("speed"));
        fillSoundsMenu();
    }
    
    public CharacterActionForm(DraggableImageComponent dic, MainMenu main, int id, int speed, String sound, String acc) {
        modo = "Editar";
        accion = acc;
        actionId = id;
        initComponents();
        setButtonImages();
        this.getContentPane().setBackground(Color.white);
        imageComponent = dic;
        menu = main;
        btnWalkForward.doClick();
        jSlider1.setValue(speed);
        lblVelocityValue.setText(speed+"");
        fillSoundsMenu(sound);
        clickButton(acc);
    }
    
    private void setImageToButton(JButton button, String imagePath){
        ImageIcon imgIcn = (new javax.swing.ImageIcon(getClass().getResource(imagePath)));
        button.setIcon(new ImageIcon(imgIcn.getImage().getScaledInstance(80, 80, Image.SCALE_SMOOTH)));
        button.setOpaque(false);
        button.setContentAreaFilled(false);
        button.setBorderPainted(false);
        button.setVerticalTextPosition(SwingConstants.BOTTOM);
        button.setHorizontalTextPosition(SwingConstants.CENTER);
    }
    
    private void clickButton(String accion){
        switch(accion){
            case "Adelante":
                btnWalkForward.doClick();
            break;
            case "Atras":
                btnWalkBackwards.doClick();
            break;
            case "Saltar":
                btnJump.doClick();
            break;
            case "Hablar":
                btnTalk.doClick();
            break;
            case "Voltear":
                btnTurn.doClick();
            break;
            default:
                btnWalkForward.doClick();
            break;
        }
    }
    
    private void setImageToButton(JToggleButton button, String imagePath){
        ImageIcon imgIcn = (new javax.swing.ImageIcon(getClass().getResource(imagePath)));
        button.setIcon(new ImageIcon(imgIcn.getImage().getScaledInstance(80, 80, Image.SCALE_SMOOTH)));
        button.setOpaque(false);
        button.setContentAreaFilled(false);
        button.setBackground(Color.lightGray);
        button.setBorderPainted(false);
        button.setVerticalTextPosition(SwingConstants.BOTTOM);
        button.setHorizontalTextPosition(SwingConstants.CENTER);
        
        button.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                hideBorder();
                JToggleButton source = (JToggleButton)e.getSource();
                source.setContentAreaFilled(true);
                accion = source.getText();
               // System.out.println(accion);
                if(accion == "Hablar"){
                    jSlider1.setVisible(false);
                    lblVelocity.setVisible(false);
                    lblVelocityValue.setVisible(false);
                    cbxSounds.setVisible(true);
                    btnPreview.setVisible(true);
                }else{
                    jSlider1.setVisible(true);
                    lblVelocity.setVisible(true);
                    lblVelocityValue.setVisible(true);
                    cbxSounds.setVisible(false);
                    btnPreview.setVisible(false);
                    if(accion == "Voltear"){
                        jSlider1.setVisible(false);
                        lblVelocity.setVisible(false);
                        lblVelocityValue.setVisible(false);
                    }
                }
            }
        });  
    }
    
    private void fillSoundsMenu(){
        cbxSounds.removeAllItems();
        ResourceCharacterLocation rcl   = new ResourceCharacterLocation();    
        ArrayList<String> audios = rcl.loadSoundArrayList();
        for (int i = 0; i < audios.size(); i++){
            cbxSounds.addItem(audios.get(i));
        }
    }
    
    private void fillSoundsMenu(String name){
        cbxSounds.removeAllItems();
        ResourceCharacterLocation rcl   = new ResourceCharacterLocation();    
        ArrayList<String> audios = rcl.loadSoundArrayList();
        for (int i = 0; i < audios.size(); i++){
            cbxSounds.addItem(audios.get(i));
            if(audios.get(i) == name){
                cbxSounds.setSelectedIndex(cbxSounds.getItemCount()-1);
            }
        }
    }
    
    private void hideBorder(){
        btnWalkForward.setContentAreaFilled(false);
        btnWalkBackwards.setContentAreaFilled(false);
        btnSave.setContentAreaFilled(false);
        btnCancel.setContentAreaFilled(false);
        btnJump.setContentAreaFilled(false);
        btnTalk.setContentAreaFilled(false);
        btnTurn.setContentAreaFilled(false);
    }

    private void setButtonImages(){
        setImageToButton(btnWalkForward, "/View/assets/caminar.png");
        setImageToButton(btnWalkBackwards, "/View/assets/retroceder.png");
        setImageToButton(btnSave, "/View/assets/guardar.png");
        setImageToButton(btnCancel, "/View/assets/equis2.png");
        setImageToButton(btnJump, "/View/assets/saltar.png");
        setImageToButton(btnTalk, "/View/assets/hablar.png");
        setImageToButton(btnTurn, "/View/assets/vuelta.png");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton6 = new javax.swing.JButton();
        jSlider1 = new javax.swing.JSlider();
        lblVelocity = new javax.swing.JLabel();
        btnWalkForward = new javax.swing.JToggleButton();
        btnWalkBackwards = new javax.swing.JToggleButton();
        btnJump = new javax.swing.JToggleButton();
        btnTurn = new javax.swing.JToggleButton();
        btnTalk = new javax.swing.JToggleButton();
        btnCancel = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        lblVelocityValue = new javax.swing.JLabel();
        cbxSounds = new javax.swing.JComboBox();
        btnPreview = new javax.swing.JButton();

        jButton6.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));
        setResizable(false);

        jSlider1.setMaximum(5);
        jSlider1.setValue(0);
        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });

        lblVelocity.setFont(new java.awt.Font("Trebuchet MS", 0, 24)); // NOI18N
        lblVelocity.setText("Velocidad:");

        btnWalkForward.setText("Adelante");
        btnWalkForward.setToolTipText("Caminar Adelante");

        btnWalkBackwards.setText("Atras");
        btnWalkBackwards.setToolTipText("Caminar Atras");

        btnJump.setText("Saltar");
        btnJump.setToolTipText("Saltar");

        btnTurn.setText("Voltear");
        btnTurn.setToolTipText("Voltear");

        btnTalk.setText("Hablar");
        btnTalk.setToolTipText("Hablar");

        btnCancel.setText("Cancelar");
        btnCancel.setToolTipText("Cancelar");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnSave.setText("Guardar");
        btnSave.setToolTipText("Guardar");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        lblVelocityValue.setFont(new java.awt.Font("Trebuchet MS", 0, 24)); // NOI18N
        lblVelocityValue.setText("0");

        cbxSounds.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        btnPreview.setText("Escuchar");
        btnPreview.setToolTipText("Escuchar");
        btnPreview.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPreviewActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnWalkForward, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnWalkBackwards, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnJump, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblVelocity)
                                .addGap(18, 18, 18)
                                .addComponent(lblVelocityValue))
                            .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cbxSounds, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnPreview)))
                        .addGap(37, 37, 37)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                    .addComponent(btnTalk, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnTurn, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnWalkForward, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnWalkBackwards, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnJump, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnTurn, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnTalk, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblVelocity)
                            .addComponent(lblVelocityValue))
                        .addGap(18, 18, 18)
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbxSounds, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnPreview, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(23, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(36, 36, 36))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider1StateChanged
        // TODO add your handling code here:
        lblVelocityValue.setText(jSlider1.getValue()+"");
    }//GEN-LAST:event_jSlider1StateChanged

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add action
        String sound = "";
        if(accion == "Hablar"){
            sound = cbxSounds.getSelectedItem().toString();
        }
        if(modo=="Crear"){
            menu.addActionDraggableObject(imageComponent.getid(), jSlider1.getValue(), accion, sound, selectedCharacterType);
        }
        
        if(modo=="Editar"){
            menu.updateActionDraggableObject(imageComponent.getid(),this.actionId, jSlider1.getValue(), accion, sound);
            menu.updateActions();
        }
       // System.out.println(""+imageComponent.getid()+"-"+jSlider1.getValue()+"-"+accion);
        this.dispose();
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnPreviewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPreviewActionPerformed
        // TODO add your handling code here:
        PlayMP3 checker = new PlayMP3();
        checker.play(cbxSounds.getSelectedItem().toString());
    }//GEN-LAST:event_btnPreviewActionPerformed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JToggleButton btnJump;
    private javax.swing.JButton btnPreview;
    private javax.swing.JButton btnSave;
    private javax.swing.JToggleButton btnTalk;
    private javax.swing.JToggleButton btnTurn;
    private javax.swing.JToggleButton btnWalkBackwards;
    private javax.swing.JToggleButton btnWalkForward;
    private javax.swing.JComboBox cbxSounds;
    private javax.swing.JButton jButton6;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JLabel lblVelocity;
    private javax.swing.JLabel lblVelocityValue;
    // End of variables declaration//GEN-END:variables
}
